package com.grantz.function.bbs.core.controller;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.grantz.function.bbs.core.entity.Discuss;
import com.grantz.function.bbs.core.inter.DiscussService;
import com.grantz.model.bbs.DiscussModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/v1/discusses")
@Api(value="论点接口",tags={"论点接口"})
public class DiscussController {

	@Resource
	private DiscussService  discussService;

	@ApiOperation(value = "保存评论，论点")
	@PutMapping
	public ResponseEntity<?>  save(@RequestBody DiscussModel model){
		try {
			discussService.save(pop(new Discuss() ,model));
		} catch (Exception e) {
			return new ResponseEntity<>(HttpStatus.CREATED);
		}
		return new ResponseEntity<>(HttpStatus.NOT_FOUND);
	}

	@ApiOperation(value = "更新评论，论点")
	@PostMapping
	public ResponseEntity<?>  update(@RequestBody DiscussModel model){
		Optional<Discuss> option = discussService.findOneById(model.getId());
		if(option.isPresent()) {
			discussService.save(pop(option.get() , model));
		}
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	private Discuss pop(Discuss dis ,DiscussModel model) {
		dis.setSsoId(model.getSsoId());
		dis.setCreatedDate(model.getCreatedDate());
		dis.setDiscuss(model.getDiscuss());
		dis.setDiscussType(model.getDiscussType());
		dis.setTitle(model.getTitle());
		dis.setKeyWords(model.getKeyWords());
		dis.setPraiseQuantity(model.getPraiseQuantity());
		dis.setCommentQuantity(model.getCommentQuantity());
		dis.setBigType(model.getBigType());
		dis.setMidType(model.getMidType());
		dis.setSmallType(model.getSmallType());
		dis.setIsHighQuality(model.getIsHighQuality());
		dis.setIsShow(model.getIsShow());
		dis.setDiscussPayway(model.getDiscussPayway());
		return dis;
	}
}
